package sing.rabbit.consumer.config;

import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import sing.rabbit.consumer.usage.DirectQueueListener;

import static sing.rabbit.common.configurations.DirectConfiguration.DIRECT_QUEUE_NAME;

/**
 * 简单模式 、 工作模式 区别在于消费者数量，队列数量都为 1，<br>
 * 没有指定交换机, 此时到默认交换机， routingKey 使用 queue 名 <br>
 * 在 SpringBoot 中，无需关心指定 routingKey 的事，配置好本身的配置就好，因为已经是说是 Simplexxx 了，做了区分
 *
 * @author xing
 * @date 2022/9/7 17:21
 */
@Configuration
public class DirectListenerConfiguration {

    @Bean
    //@ConditionalOnBean({CachingConnectionFactory.class, SimpleQueueListener.class})
    public DirectMessageListenerContainer simpleMessageListenerContainer(CachingConnectionFactory rabbitConnectionFactory, DirectQueueListener directQueueListener) {
        DirectMessageListenerContainer container = new DirectMessageListenerContainer();
        container.setConnectionFactory(rabbitConnectionFactory);
        container.setQueueNames(DIRECT_QUEUE_NAME);
        container.setMessageListener(directQueueListener);
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        return container;
    }

}
